package com.shixun.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.Element;
import com.shixun.dao.BaseDao;
import com.shixun.entity.FileEntity;
import com.shixun.entity.FpinglunEntity;
import com.shixun.entity.UserEntity;
import com.shixun.service.ZhyService;
import org.apache.commons.chain.web.WebContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.concurrent.SuccessCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by haiyan on 2016/12/12.
 */
@Controller
@Scope("prototype")
@ParentPackage("struts-default")
@Namespace("/shixun")
public class ZhyAction extends ActionSupport {

    @Autowired
    private UserEntity userEntity;
    @Autowired
    private FileEntity fileEntity;
    @Autowired
    private ZhyService zhyService;

    /**
     * 文章的发布
     * @return
     */
    @Action(value = "fileAction",results = {
            @Result(location = "selectFourInfo",type = "redirect"),
            @Result(name = "error",location = "/userLogin/fabiaoFile.jsp")
    })
    public String fileAction(){
        System.out.println("00");
        if (zhyService.saveFileService(fileEntity)){
            return SUCCESS;
        }
        return ERROR;
    }
    /**
     * 查找四条文章信息
     */
    @Action(value = "selectFourInfo",results = {
            @Result(location = "/userLogin/wenzhang.jsp"),
            @Result(name = "error",location = "/userLogin/wenzhang.jsp")
    })
    public String selectFourInfo(){
        List<FileEntity> list = zhyService.searchAllFileService();
        List<FileEntity> flist = new ArrayList<>();
        if (list != null && !list.isEmpty()){
            if (list.size() > 4){
                for (int i = 0;i < list.size();i++){
                    flist.add(list.get(i));
                    if (flist.size() == 4){
                        break;
                    }
                }
                BaseDao.getRequest().setAttribute("searchSuccess",flist);
                ActionContext.getContext().put("searchOneInfo",list.get(list.size()-1));
                return SUCCESS;
            }else{
                BaseDao.getRequest().setAttribute("searchSuccess",list);
                ActionContext.getContext().put("searchOneInfo",list.get(list.size()-1));
                return SUCCESS;
            }
        }else {
            ActionContext.getContext().put("searchFaile","您还没有发表任何文章");
            return ERROR;
        }
    }

    /**
     * 查找所有的文章信息
     * @return
     */
    @Action(value = "searchAllfile",results = {
            @Result(location = "/userLogin/wenzhang.jsp"),
            @Result(name = "error",location = "userLogin/wenzhang.jsp")
    })
    public String searchAllfile(){
        List<FileEntity> list = zhyService.searchAllFileService();
        if (list.size() > 0){
            ActionContext.getContext().put("hiddenMore","隐藏更多标签");
            BaseDao.getRequest().setAttribute("searchSuccess",list);
            ActionContext.getContext().put("searchOneInfo",list.get(list.size()-1));
            return SUCCESS;
        }else {
            ActionContext.getContext().put("searchFaile","您还没有发表任何文章");
            return ERROR;
        }
    }

    /**
     * 按照id查找一条文章信息
     * @return
     */
    @Action(value = "searchOneFile",results = {
            @Result(location = "/userLogin/filexiangqin.jsp"),
            @Result(name = "error",location = "/userLogin/wenzhang.jsp")
    })
    public String searchOneFile(){
        FileEntity fe = zhyService.serarchOneFileService(BaseDao.getRequest().getParameter("fileid"));
        List<FpinglunEntity> flist = zhyService.selectAllPinLun(BaseDao.getRequest().getParameter("fileid"));
        if (fe == null){
            ActionContext.getContext().put("searchFaile","您的网络出现问题，请稍候重试");
            return ERROR;
        }else {
            if (flist.size() > 0){
                BaseDao.getRequest().setAttribute("searchAllPinlun",flist);
            }
            System.out.println("++++++++::::" + fe.getUserEntity().getName());
            fe.setFtime(fe.getFtime());
            ActionContext.getContext().put("searchOneSuccess",fe);   //request,session,actionContent都是在传值和er表达式（${}）的事上都是同样的
            return SUCCESS;
        }
    }

    /**
     * 点赞 数量的传输
     * @return
     */
    @Action(value = "dianZanNumber")
    public void dianZanNumber(){
        System.out.println("*******");
        zhyService.wenZhangDianZan(BaseDao.getRequest().getParameter("dianZan"));
    }

    /**
     * 留言
     * @return
     */
    @Action(value = "liuyanAdd")
    public void liuyanAdd(){
        zhyService.saveLiuYan(BaseDao.getRequest().getParameter("wzId"),BaseDao.getRequest().getParameter("lyMain"));
    }

    /**
     * 跳转首页
     * @return
     */
    @Action(value = "indexLogin",results = {
            @Result(location = "/userLogin/indexLogin.jsp"),
            @Result(name = "error",location = "/userLogin/indexLogin.jsp")
    })
    public String indexLogin(){
        List<FileEntity> list = zhyService.searchFileSix();
        if (list.size() > 0){
            System.out.println("++++++++:::" + list.size());
            ActionContext.getContext().put("sixInfo",list);
            return SUCCESS;
        }else {
            BaseDao.getRequest().setAttribute("sixTishi","你还没有发表任何文章");
            return ERROR;
        }
    }

    /**
     * 跳转我的主页面
     * @return
     */
    @Action(value = "myselfMain",results = {
            @Result(location = "/userLogin/myself.jsp"),
            @Result(name = "error",location = "/userLogin/myself.jsp")
    })
    public String myselfMain(){
        List<FileEntity> listWenZhang = zhyService.selectMyselfFile();
        List<FileEntity> listTieZi = zhyService.selectMyselfTiezi();
        ActionContext.getContext().put("WZlist",listWenZhang);
        ActionContext.getContext().put("TZlist",listTieZi);
        return SUCCESS;
    }

    /**
     * 按照id查找一条文章信息  这是个人主页
     * @return
     */
    @Action(value = "searchOneFileOfMyself",results = {
            @Result(location = "/userLogin/filexiangqin.jsp"),
            @Result(name = "error",location = "/userLogin/myself.jsp")
    })
    public String searchOneFileOfMyself(){
        FileEntity fe = zhyService.serarchOneFileService(BaseDao.getRequest().getParameter("fileid"));
        List<FpinglunEntity> flist = zhyService.selectAllPinLun(BaseDao.getRequest().getParameter("fileid"));
        if (fe == null){
            ActionContext.getContext().put("searchFaile","您的网络出现问题，请稍候重试");
            return ERROR;
        }else {
            if (flist.size() > 0){
                BaseDao.getRequest().setAttribute("searchAllPinlun",flist);
            }
            fe.setFtime(fe.getFtime());
            ActionContext.getContext().put("searchOneSuccess",fe);   //request,session,actionContent都是在传值和er表达式（${}）的事上都是同样的
            return SUCCESS;
        }
    }

    /**
     * 删除一条文件记录
     * @return
     */
    @Action(value = "delteOneFile",results = {
            @Result(location = "/userLogin/myself.jsp"),
            @Result(name = "error",location = "/userLogin/myself.jsp")
    })
    public String delteOneFile(){
        if (zhyService.delFile(BaseDao.getRequest().getParameter("fileid"))){
            return SUCCESS;
        }else {
            return ERROR;
        }
    }

//这是未登录钱的所有跳转
    /****
     * 未登录时获取首页文章
     * @return
     */
    @Action(value = "shouyeInfo",results = {
            @Result(location = "/fristpage.jsp"),
            @Result(name = "error",location = "/fristpage.jsp")
    })
    public String shouyeInfo(){
        List<FileEntity> list = zhyService.searchFileSix();
        if (list != null && !list.isEmpty()){
            System.out.println("++++++++:::" + list.size());
            ActionContext.getContext().put("sixInfo",list);
            return SUCCESS;
        }else {
            BaseDao.getRequest().setAttribute("sixTishi","你还没有发表任何文章");
            return ERROR;
        }
    }
    /**
     * 未登录时获取文章
     * @return
     */
    @Action(value = "loginAgeFile",results = {
            @Result(location = "/user/wenzhang.jsp"),
            @Result(name = "error",location = "/user/wenzhang.jsp")
    })
    public String loginAgeFile(){
        List<FileEntity> list = zhyService.searchAllFileService();
        List<FileEntity> flist = new ArrayList<>();
        if (list!=null && !list.isEmpty()){
            if (list.size() > 4){
                for (int i = 0;i < list.size();i++){
                    flist.add(list.get(i));
                    if (flist.size() == 4){
                        break;
                    }
                }
                System.out.println("消除重复");
                BaseDao.getRequest().setAttribute("searchSuccess",flist);
                ActionContext.getContext().put("searchOneInfo",list.get(list.size()-1));
                return SUCCESS;
            }else{
                BaseDao.getRequest().setAttribute("searchSuccess",list);
                ActionContext.getContext().put("searchOneInfo",list.get(list.size()-1));
                return SUCCESS;
            }
        }else {
            ActionContext.getContext().put("searchFaile","您还没有发表任何文章");
            return ERROR;
        }
    }

    /**
     * 跳转详细的页面
     */
    @Action(value = "ageXiangXi",results = {
            @Result(location = "/user/agefile.jsp"),
            @Result(name = "error",location = "/user/agefile.jsp")
    })
    public String ageXiangXi(){
        FileEntity fe = zhyService.serarchOneFileService(BaseDao.getRequest().getParameter("fileid"));
        List<FpinglunEntity> flist = zhyService.selectAllPinLun(BaseDao.getRequest().getParameter("fileid"));
        if (fe == null){
            ActionContext.getContext().put("searchFaile","您的网络出现问题，请稍候重试");
            return ERROR;
        }else {
            if (flist.size() > 0){
                BaseDao.getRequest().setAttribute("searchAllPinlun",flist);
            }
            System.out.println();
            fe.setFtime(fe.getFtime());
            ActionContext.getContext().put("searchOneSuccess",fe);//request,session,actionContent都是在传值和er表达式（${}）的事上都是同样的
            return SUCCESS;
        }
    }

    /**
     * 未登录前查找所有的文章信息
     */
    @Action(value = "ageSelectAllFile",results = {
            @Result(location = "/user/wenzhang.jsp"),
            @Result(name = "error",location = "/user/wenzhang.jsp")
    })
    public String ageSelectAllFile(){
        List<FileEntity> list = zhyService.searchAllFileService();
        if (list.size() > 0){
            System.out.println();
            ActionContext.getContext().put("hiddenMore","隐藏更多标签");
            BaseDao.getRequest().setAttribute("searchSuccess",list);
            ActionContext.getContext().put("searchOneInfo",list.get(list.size()-1));
            return SUCCESS;
        }else {
            ActionContext.getContext().put("searchFaile","您还没有发表任何文章");
            return ERROR;
        }
    }

    /**
     * 差找所有的用户信息
     * @return
     */
    @Action(value = "allUserInfo",results = {
            @Result(location = "/adminLogin/memberManager.jsp")
    })
    public String allUserInfo(){
        List<UserEntity> listUser = zhyService.selectAllUser();
        ActionContext.getContext().put("allUser",listUser);
        return SUCCESS;
    }

    /**
     * 改变用户的状态为0
     * @return
     */
    @Action(value = "changeUserState",results = {
        @Result(location = "/adminLogin/memberManager.jsp"),
        @Result(name = "error",location = "/adminLogin/memberManager.jsp")
    })
    public String changeUserState(){
        List<UserEntity> list = zhyService.oneUserInfo(BaseDao.getRequest().getParameter("userId"));
        if(list != null && !list.isEmpty()){
            for (int i = 0;i < list.size();i++){
                list.get(i).setMore2(list.get(i).getCreatetime().toString().substring(0,10));
            }
            ActionContext.getContext().put("allUser",list);
            ActionContext.getContext().put("changeUser","删除成功");
            return SUCCESS;
        }else {
            ActionContext.getContext().put("changeUser","删除失败");
            return ERROR;
        }
    }

    /**
     * 查找所有文章
     * @return
     */
    @Action(value = "allFile",results = {
            @Result(location = "/adminLogin/articleManager.jsp"),
            @Result(name="error",location = "/adminLogin/articleManager.jsp")
    })
    public String allFile(){
        List<FileEntity> listF = zhyService.searchAllFileService();
        if (listF != null && !listF.isEmpty()){
            for (int i = 0;i < listF.size();i++){
                listF.get(i).setFphotoName(listF.get(i).getFtime().toString().substring(0,10));
                listF.get(i).setFphoneAddress(listF.get(i).getId().substring(10,32));
            }
            ActionContext.getContext().put("listF",listF);
            return SUCCESS;
        }
        return ERROR;
    }

    /**
     * 查找所有的帖子
     * @return
     */
    @Action(value = "Tiezi",results = {
            @Result(location = "/adminLogin/postManager.jsp"),
            @Result(name = "error",location = "/adminLogin/postManager.jsp")

    })
    public String Tiezi(){
        List<FileEntity> list = zhyService.allTizi();
        if (list != null && !list.isEmpty()){
            for (int i = 0;i < list.size();i++){
                list.get(i).setFphotoName(list.get(i).getFtime().toString().substring(0,10));
                list.get(i).setFphoneAddress(list.get(i).getId().substring(10,32));
            }
            ActionContext.getContext().put("listTiezi",list);
            return SUCCESS;
        }
        return ERROR;
    }

    /**
     * 修改用户状态为1
     * @return
     */
    @Action(value = "changeUser",results = {
        @Result(location = "/adminLogin/memberManager.jsp"),
        @Result(name = "error",location = "/adminLogin/memberManager.jsp")
    })
    public String changeUser(){
        List<UserEntity> list = zhyService.changUser(BaseDao.getRequest().getParameter("userid"));
        if (list != null && !list.isEmpty()){
            for (int i = 0;i < list.size();i++){
                list.get(i).setMore2(list.get(i).getCreatetime().toString().substring(0,10));
            }
            ActionContext.getContext().put("allUser",list);
            ActionContext.getContext().put("changeUser","该用户已经有效");
            return SUCCESS;
        }else {
            ActionContext.getContext().put("changeUser","该用户已经有效");
            return ERROR;
        }
    }

    /**
     * 注销管理员
     * @return
     */
    @Action(value = "zhuxiaoAdmin",results = {
            @Result(location = "shouyeInfo",type = "redirect")
    })
    public String zhuxiaoAdmin(){
        Map session = ActionContext.getContext().getSession();
        session.clear();
        return SUCCESS;
    }


    public UserEntity getUserEntity() {
        return userEntity;
    }

    public void setUserEntity(UserEntity userEntity) {
        this.userEntity = userEntity;
    }

    public FileEntity getFileEntity() {
        return fileEntity;
    }

    public void setFileEntity(FileEntity fileEntity) {
        this.fileEntity = fileEntity;
    }
}
